/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hello;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.media.*;
import  java.io.InputStream;
import javax.microedition.media.control.*;
/**
 *
 * @author THANH PHUC
 */
public class JumpCanvas extends GameCanvas{

    static int DISP_WIDTH;
    static int DISP_HEIGHT;
    static int CORNER_X = 0;
    static int CORNER_Y = 0;
    static int FONT_HEIGHT;
    static HelloMIDlet myJump;
    static Font FONT;
    static final int GROUND_HEIGHT = 32;
    static int myScore = 0;
    static int myGameTicks  = 950;
    private Display myDisplay;
    
    private JumpManager myManager;

    public static final int BLACK = 0;
    public static final int WHITE = 0xffffff;
     Player player;
    public JumpCanvas(HelloMIDlet midlet) throws Exception {
        super(false);
        myJump = midlet;
        
        myDisplay = Display.getDisplay(midlet);
        DISP_WIDTH = getWidth();
        DISP_HEIGHT = getHeight();
        FONT = getGraphics().getFont();
        FONT_HEIGHT = FONT.getHeight();

        //Display disp = Display.getDisplay(myJump);

        if(myManager == null)
        {
            /*
            myManager = new JumpManager(CORNER_X, CORNER_Y + FONT_HEIGHT*2, 
	   DISP_WIDTH, DISP_HEIGHT - FONT_HEIGHT*2 - GROUND_HEIGHT);
           */
            myManager = new JumpManager(0,0,DISP_WIDTH,DISP_HEIGHT);
        }
    }
    
     /**
   * This is called as soon as the application begins.
   */
    void start() {
    //myGameOver = false;
    myDisplay.setCurrent(this);
     //playMusic();
    repaint();
    
    }
void playMusic() {
    try {
    // create an input stream that can be used to read the
    // music data from a file called music.wav in a directory
    // called resources in this program's JAR file.
    InputStream is = getClass().getResourceAsStream("/images/song.mid");
    // create a media player to play the music file, and
    // inform the player that the format is "audio/X-wav"
     player = Manager.createPlayer(is, "audio/midi");
    // start the music
     
player.setLoopCount(-1);
    player.start();
    
    } 
     catch (Exception ioe) 
    {
    // deal with the exception
        throw null;
    } 
}

    /**
    * sets all variables back to their initial positions.
    */
    void reset() {
    //myManager.reset();
    //myScore = 0;
    //myGameOver = false;
    //myGameTicks = myInitialGameTicks;
    //myOldGameTicks = myInitialGameTicks;
    repaint();
    }
    
    void flushKeys() {
    getKeyStates();
    }
    
     void advance() {
    //myGameTicks--;
    myScore += myManager.advance(myGameTicks);
         /*
    if(myGameTicks == 0) {
      setGameOver();
    }*/
    // paint the display
    try {
      paint(getGraphics());
      flushGraphics();
    } catch(Exception e) {
//      myJump.errorMsg(e);
    }
  }

     
    public void paint(Graphics g) {
        g.setColor(WHITE);
        g.fillRect(CORNER_X, CORNER_Y, DISP_WIDTH, DISP_HEIGHT);
        /*
        // color the grass green
        g.setColor(0, 255, 0);
        g.fillRect(CORNER_X, CORNER_Y + DISP_HEIGHT - GROUND_HEIGHT, 
                   DISP_WIDTH, DISP_HEIGHT);
        */
        try {
          myManager.paint(g);
        } catch(Exception e) {
          //myJump.errorMsg(e);
        }

    }

    protected void keyReleased(int arg0) {
        super.keyReleased(arg0);
        if((arg0 & LEFT_PRESSED)!=0)
        {
            myManager.setAction(0);
            myManager.advance(myGameTicks);
        }
    }
    
    
    public void checkKeys() 
    { 

          int keyState = getKeyStates();
         
          if((keyState & RIGHT_PRESSED) != 0) {
            myManager.setAction(1);
          }
          if((keyState & LEFT_PRESSED) != 0) {
            myManager.setAction(2);
          } 
          if((keyState & UP_PRESSED) != 0) {
            myManager.setAction(3);
          } 
          if((keyState & DOWN_PRESSED) != 0) {
            myManager.setAction(4);
          }   
             if((keyState & FIRE_PRESSED) != 0) 
             {       
                myManager.AppendBomb();   
            }
    }
    public void DeleteAllComponent()
    {
        try
        {
        player.stop();
        player = null;
        myManager.DeleteAllComponent();
        myManager = null;
        }
        catch( Exception ex)
        {
            throw null;
        }
    }

}
